<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">来信须知</span></el-col>
      </el-row>
      <el-table v-loading="loading" :data="instructionsList" border class="mt20">
        <el-table-column type="index" width="60" align="center" label="序号" fixed="left" />
        <el-table-column label="标题" min-width="500" align="center" prop="name" />
        <el-table-column label="操作" width="280" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['wms:instructions:edit']">修改</el-button>
            <el-button link type="primary" icon="View" @click="handleSee(scope.row)"
              v-hasPermi="['wms:instructions:edit']">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>

    </el-card>
    <!-- 添加或修改来信须知对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="instructionsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="name">
          <el-input disabled v-model="form.name" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content" v-if="form.id!='1867802979512750083'">
          <editor v-model="form.content" height="650" />
        </el-form-item>
        <el-form-item label="后台标题" prop="content" v-if="form.id=='1867802979512750083'">
          <el-input  v-model="form.content" placeholder="请输入后台标题" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
    <el-drawer title="查看详情" v-model="openSee" size="50%" append-to-body>
      <el-form ref="instructionsRef" :model="formSee" :rules="rules" label-width="80px">
        <el-form-item label="标题">
          <el-input disabled v-model="formSee.name" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" v-if="formSee.id!='1867802979512750083'">
          <editor disabled v-model="formSee.content" height="650" />
        </el-form-item>
        <el-form-item label="后台标题" v-if="formSee.id=='1867802979512750083'">
          <el-input disabled v-model="formSee.content" placeholder="请输入后台标题" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openSee=false">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Instructions">
  import {
    listInstructions,
    getInstructions,
    delInstructions,
    addInstructions,
    updateInstructions
  } from "@/api/wms/instructions";

  const {
    proxy
  } = getCurrentInstance();

  const instructionsList = ref([]);
  const open = ref(false);
  const buttonLoading = ref(false);
  const loading = ref(true);


  const openSee = ref(false);
  const ids = ref([]);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    formSee: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined,
      content: undefined,
    },
    rules: {
      id: [{
        required: true,
        message: "不能为空",
        trigger: "blur"
      }],
      name: [{
        required: true,
        message: "标题不能为空",
        trigger: "blur"
      }],
      content: [{
        required: true,
        message: "内容不能为空",
        trigger: "blur"
      }],
    }
  });

  const {
    queryParams,
    form,
    formSee,
    rules
  } = toRefs(data);

  /** 查询来信须知列表 */
  function getList() {
    loading.value = true;
    listInstructions(queryParams.value).then(response => {
      instructionsList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      name: null,
      content: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null
    };
    proxy.resetForm("instructionsRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加来信须知";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getInstructions(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改来信须知";
    });
  } /** 查看按钮操作 */
  function handleSee(row) {
    formSee.value = {}
    const _id = row.id || ids.value
    getInstructions(_id).then(response => {
      formSee.value = response.data;
      openSee.value = true;

    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["instructionsRef"].validate(valid => {
      if (valid) {
        buttonLoading.value = true;
        if (form.value.id != null) {
          updateInstructions(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        } else {
          addInstructions(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          }).finally(() => {
            buttonLoading.value = false;
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除来信须知编号为"' + _ids + '"的数据项？').then(function() {
      loading.value = true;
      return delInstructions(_ids);
    }).then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {}).finally(() => {
      loading.value = false;
    });
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('wms/instructions/export', {
      ...queryParams.value
    }, `instructions_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
